import numpy as np
import math
import requests
import xgboost


def global_coefficient(m1, m2):
    L1 = np.triu(m1, 1)
    L2 = np.triu(m2, 1)
    means1 = np.mean(L1[L1.nonzero()])
    means2 = np.mean(L2[L2.nonzero()])
    print(L1)
    numerator, denominator1, denominator2 = [], [], []
    [rows, cols] = L1.shape
    for i in range(rows - 1):
        for j in range(cols - 1):
            if j > i:
                print(L1[i, j])
                numerator.append((L1[i, j] - means1) * (L2[i, j] - means2))
                denominator1.append(abs(L1[i, j] - means1) ** 2)
                denominator2.append(abs(L2[i, j] - means2) ** 2)
        print('-----------------------')
    denominator = math.sqrt(sum(denominator1)) * math.sqrt(sum(denominator2))
    GTDCC = sum(numerator) / denominator
    return GTDCC


def global_coefficient1(m1, m2):
    L1 = np.triu(m1, 1)
    L2 = np.triu(m2, 1)
    A = L1[L1.nonzero()]
    B = L2[L2.nonzero()]
    meansA = np.mean(A)
    meansB = np.mean(B)
    numerator = sum((A-meansA)*(B-meansB))
    denominator =  math.sqrt(sum(abs(A-meansA)**2) * sum(abs(B-meansB)**2))
    GTDCC = numerator / denominator
    return GTDCC



def single_coefficient(m1, m2, n):
    L1 = np.triu(m1, 1)
    L2 = np.triu(m2, 1)
    # meansA = np.mean(L1[L1.nonzero()])
    # meansB = np.mean(L2[L2.nonzero()])
    # L1 = np.triu(m1, 1)[:n]
    # L2 = np.triu(m2, 1)[:n]
    L1 = np.triu(m1, 1)[n-1:n]
    L2 = np.triu(m2, 1)[n-1:n]
    A = L1[L1.nonzero()]
    B = L2[L2.nonzero()]

    meansA = np.mean(A)
    meansB = np.mean(B)

    numerator = sum((A - meansA) * (B - meansB))
    denominator = math.sqrt(sum(np.square(A - meansA)) * sum(np.square(B - meansB)))
    GTDCC = numerator / denominator
    return GTDCC


def single_coefficient1(m1, m2, n):
    L1 = np.triu(m1, 1)
    L2 = np.triu(m2, 1)
    means1 = np.mean(L1[L1.nonzero()])
    means2 = np.mean(L2[L2.nonzero()])
    print(L1)
    numerator, denominator1, denominator2 = [], [], []
    [rows, cols] = L1.shape
    for i in range(n - 1):
        for j in range(cols - 1):
            if j > i:
                print(L1[i, j])
                numerator.append((L1[i, j] - means1) * (L2[i, j] - means2))
                denominator1.append(abs(L1[i, j] - means1) ** 2)
                denominator2.append(abs(L2[i, j] - means2) ** 2)
        print('-----------------------')
    denominator = math.sqrt(sum(denominator1)) * math.sqrt(sum(denominator2))
    GTDCC = sum(numerator) / denominator
    return GTDCC


def get_node_vec(m1, m2, n):
    pass


if __name__ == "__main__":
    m1 = np.mat(
        "0,2,2,2,1,1,1,2,2,2;0,0,2,4,3,3,1,4,4,4;0,0,0,4,3,3,1,4,4,4;0,0,0,0,3,1,3,4,2,4;0,0,0,0,0,2,2,1,3,1;0,0,0,0,0,0,2,3,1,3;0,0,0,0,0,0,0,3,3,3;0,0,0,0,0,0,0,0,4,2;0,0,0,0,0,0,0,0,0,4;0,0,0,0,0,0,0,0,0,0")
    m2 = np.mat(
        "0,2,2,1,1,1,1,2,1,2;0,0,2,2,3,3,1,4,3,4;0,0,0,3,3,3,1,4,3,4;0,0,0,0,2,2,2,3,2,3;0,0,0,0,0,2,2,1,2,1;0,0,0,0,0,0,2,3,2,3;0,0,0,0,0,0,0,3,2,3;0,0,0,0,0,0,0,0,3,2;0,0,0,0,0,0,0,0,0,3;0,0,0,0,0,0,0,0,0,0")


    g1= np.mat("0,1,1,2,2,2,2;0,0,2,1,1,3,3;0,0,0,3,3,1,1;0,0,0,0,2,4,4;0,0,0,0,0,4,4;0,0,0,0,0,0,2;0,0,0,0,0,0,0")
    g2= np.mat("0,1,2,1,1,3,3;0,0,1,2,2,2,2;0,0,0,3,3,1,1;0,0,0,0,2,4,4;0,0,0,0,0,4,4;0,0,0,0,0,0,2;0,0,0,0,0,0,0")
    # GTDCC = global_coefficient1(m1, m2)
    S_GTDCC = single_coefficient(m1, m2,2)
    pass
    # interval = 5
    # command = r"ipconfig"
    # run(interval, command)
